<?php

namespace app\admin\service;

use app\admin\model\Permession;
use app\admin\model\Role as Model;
use app\admin\validate\RoleValidate as Validate;

class Role extends AdminService
{

    protected function init ()
    {
        $this->model    = new Model();
        $this->validate = new Validate();
    }

    public function afterCreate(\app\admin\model\Role $model ,$data=[]){
        $data = empty($data)?$this->request->post():$data;
        if(empty($data['permession'])){
            return true;
        }
        $permessionIds = array_unique($data['permession']);

        $insert = [];
        $roleId = $model->id;
        foreach($permessionIds as $value){
            $insert[] = [
                'role_id' => $roleId,
                'permession_id' => $value
            ];
        }

        return (new Permession)->insertAll($insert);
    }

}

